#include "i2cexp.h"
#include "usbstk5505_i2c.h"
#include "mfutils.h"
#include "data_types.h"




Uint16 rButton;
Uint16 lButton;
Uint16 uButton;
Uint16 dButton;
Uint16 arButton;

Int16 updateButtons()
{
	Int16 e=0;
	Uint16 cmd[1];
	cmd[0] = 0x13;
	e += USBSTK5505_I2C_write(BUTTON_ADDR, cmd,1);
	wait_cycles(10);
	e += USBSTK5505_I2C_read(BUTTON_ADDR, cmd, 1);
	rButton = (RBUTTON_MASK & cmd[0]) == 0;
	lButton = (LBUTTON_MASK & cmd[0]) == 0;
	uButton = (UBUTTON_MASK & cmd[0]) == 0;
	dButton = (DBUTTON_MASK & cmd[0]) == 0;
	arButton = (ARBUTTON_MASK & cmd[0]) == 0;
	wait_usec(100);
	return e;
}
Int16 pullUpBank0()
{
	Int16 e=0;
	Uint16 cmd[1];
	cmd[0] = 0x0D;
	cmd[1] = 0xFF;
	e += USBSTK5505_I2C_write(BUTTON_ADDR, cmd,2);
	wait_cycles(10);


	wait_usec(100);
	return e;
}

Uint16 get_rButton()
{
	return rButton;
}

Uint16 get_lButton()
{
	return lButton;
}

Uint16 get_dButton()
{
	return dButton;
}

Uint16 get_uButton()
{
	return uButton;
}
Uint16 push_rButton()
{
	static Uint16 prev_rButton = 0;
	if(!prev_rButton && rButton)
	{
		prev_rButton = 1;
		return 1;
	}	
	else
	{
		prev_rButton = rButton;
		return 0;
	}
	
}

Uint16 push_lButton()
{
	static Uint16 prev_lButton = 0;
	if(!prev_lButton && lButton)
	{
		prev_lButton = 1;
		return 1;
	}	
	else
	{
		prev_lButton = lButton;
		return 0;
	}
	
}

Uint16 push_uButton()
{
	static Uint16 prev_uButton = 0;
	if(!prev_uButton && uButton)
	{
		prev_uButton = 1;
		return 1;
	}	
	else
	{
		prev_uButton = uButton;
		return 0;
	}
	
}

Uint16 push_dButton()
{
	static Uint16 prev_dButton = 0;
	if(!prev_dButton && dButton)
	{
		prev_dButton = 1;
		return 1;
	}	
	else
	{
		prev_dButton = dButton;
		return 0;
	}
	
}

Uint16 push_arButton()
{
	static Uint16 prev_arButton = 0;
	if(!prev_arButton && arButton)
	{
		prev_arButton = 1;
		return 1;
	}	
	else
	{
		prev_arButton = arButton;
		return 0;
	}
	
}




